home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / Booting Gallery / Booting Gallery (source) / Sources / Blasteroids / BlasteroidsShipSprite.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-22  |  4.2 KB  |  241 lines  |  [TEXT/CWIE]

  1. /***
  2.  *     Created by Bill Hubauer on Fri, Jun 21, 1996 @ 2:53 AM.
  3.  *
  4.  ***/
  5.  
  6.  
  7. #ifndef __BlasteroidsShipSprite_H__
  8. #include "BlasteroidsShipSprite.h"
  9. #endif
  10.  
  11.  
  12. static void CalculateDeltaHDeltaV(short radius, short variant, short& deltaH, short& deltaV)
  13. {
  14.     switch (variant)
  15.     {
  16.         case 0:
  17.         case 12:
  18.             deltaV = -radius ;
  19.             deltaH = 0;
  20.             break;
  21.         case 6:
  22.         case 18:
  23.             deltaV = 0;
  24.             deltaH = radius;
  25.             break;
  26.         case 1:
  27.         case 11:
  28.         case 13:
  29.         case 23:
  30.             deltaV = -23 * radius / 25;
  31.             deltaH = 10 * radius / 25;
  32.             break;
  33.         case 2:
  34.         case 10:
  35.         case 14:
  36.         case 22:
  37.             deltaV = -20 * radius / 25;
  38.             deltaH = 15 * radius / 25;
  39.             break;
  40.         case 3:
  41.         case 9:
  42.         case 15:
  43.         case 21:
  44.             deltaV = -18 * radius / 25;
  45.             deltaH = 18 * radius / 25;
  46.             break;
  47.         case 4:
  48.         case 8:
  49.         case 16:
  50.         case 20:
  51.             deltaV = -15 * radius / 25;
  52.             deltaH = 20 * radius / 25;
  53.             break;
  54.         case 5:
  55.         case 7:
  56.         case 17:
  57.         case 19:
  58.             deltaV = -10 * radius / 25;
  59.             deltaH = 23 * radius / 25;
  60.             break;
  61.     }
  62.     
  63.     if (variant >= 18)
  64.         deltaH = -deltaH;
  65.     else if (variant >= 12)
  66.     {
  67.         deltaH = -deltaH;
  68.         deltaV = -deltaV;
  69.     }
  70.     else if (variant >= 6)
  71.         deltaV = -deltaV;
  72. }
  73.  
  74.  
  75. static short Sign(short value)
  76. {
  77.     if (value < 0)
  78.         return -1;
  79.     if (value > 0)
  80.         return 1;
  81.     return 0;
  82. }
  83.  
  84.  
  85. void    CBlasteroidsShipSprite::UpdatePosition() //Override
  86. {
  87.     if(SpaceDownQ()){
  88.         FireGun();
  89.     }
  90.     
  91.     short            newHPos = GetLocation()->left;
  92.     short            newVPos = GetLocation()->top;
  93.     const Rect&        bounds = *GetGameBounds();
  94.     
  95.     if (LeftArrowQ())
  96.         fShipVariant = (fShipVariant + 24 - 1) % 24;
  97.     else if (RightArrowQ())
  98.         fShipVariant = (fShipVariant + 24 + 1) % 24;
  99.     SetImageID(fShipId + fShipVariant);
  100.         
  101.     if (DownArrowQ())
  102.     {
  103.         PlaySound(5004);
  104.         short deltaH, deltaV;
  105.         
  106.         fSpeed += 2;
  107.         if (fSpeed < 6)
  108.             fSpeed = 6;
  109.         else if (fSpeed > 15)
  110.             fSpeed = 15;
  111.             
  112.         CalculateDeltaHDeltaV(fSpeed, fShipVariant, deltaH, deltaV);
  113.         newHPos += deltaH;
  114.         newVPos += deltaV;
  115.         fHMomentum = deltaH;
  116.         fVMomentum = deltaV;
  117.     }
  118.     
  119.     newHPos += fHMomentum;
  120.     newVPos += fVMomentum;
  121.     fHMomentum -= Sign(fHMomentum);
  122.     fVMomentum -= Sign(fVMomentum);
  123.     fSpeed -= Sign(fSpeed);
  124.     MoveTo(newHPos, newVPos);
  125.     
  126.     if (fLocation.left < bounds.left)
  127.         MoveToH(bounds.right);
  128.     if (fLocation.left > bounds.right)
  129.         MoveToH(bounds.left);
  130.     if (fLocation.top < bounds.top)
  131.         MoveToV(bounds.bottom);
  132.     if (fLocation.top >  bounds.bottom)
  133.         MoveToV(bounds.top);
  134. }
  135.  
  136.  
  137. static short CalcShipTop(CSpriteWorld* world)
  138. {
  139.     const Rect&        bounds = *(world->GetSpriteCanvas()->GetBounds());
  140.     
  141.     return (bounds.bottom + bounds.top - 32) / 2;
  142. }
  143.  
  144.  
  145. inline short    RectWidth(const Rect& r)
  146. {
  147.     return r.right - r.left;
  148. }
  149.  
  150.  
  151. static short CalcShipLeft(CSpriteWorld* world)
  152. {
  153.     const Rect&        bounds = *(world->GetSpriteCanvas()->GetBounds());
  154.  
  155.     return (bounds.left + bounds.right - 32) / 2;
  156. }
  157.  
  158.  
  159. CBlasteroidsShipSprite::CBlasteroidsShipSprite(CSpriteWorld* world,CSpriteGame* game)
  160.     :    CGameSprite(world, game, 0,game->GetImage(5000), CalcShipTop(world),
  161.              CalcShipLeft(world), game->GetImageMask(5000))
  162. {
  163.     fHMomentum = 0;
  164.     fVMomentum = 0;
  165.     fSpeed = 0;
  166.     fShipId = 5000;
  167.     fShipVariant = 0;
  168. }
  169.  
  170.  
  171. CBlasteroidsShipSprite::~CBlasteroidsShipSprite()//Override
  172. {
  173. }
  174.  
  175.  
  176. Boolean    CBlasteroidsShipSprite::LeftArrowQ()
  177. {
  178.     return KeyIsDownQ(0x7B);
  179. }
  180.  
  181.  
  182. Boolean    CBlasteroidsShipSprite::RightArrowQ()
  183. {
  184.     return KeyIsDownQ(0x7C);
  185. }
  186.  
  187.  
  188. Boolean    CBlasteroidsShipSprite::UpArrowQ()
  189. {
  190.     return KeyIsDownQ(0x7E);         
  191. }
  192.  
  193.  
  194. Boolean    CBlasteroidsShipSprite::DownArrowQ()
  195. {
  196.     return KeyIsDownQ(0x7D);
  197. }
  198.  
  199.  
  200. Boolean    CBlasteroidsShipSprite::SpaceDownQ()
  201. {
  202.     return KeyIsDownQ(0x31);
  203. }
  204.  
  205.  
  206. void    CBlasteroidsShipSprite::FireGun()
  207. {
  208.     short        deltaV,deltaH;
  209.     
  210.     CalculateDeltaHDeltaV(fSpeed+25, fShipVariant, deltaH, deltaV);
  211.     new CBlasteroidsBulletSprite(GetWorld(), GetGame(), GetLocation()->top, GetLocation()->left, 
  212.         deltaV, deltaH);
  213. }
  214.  
  215.  
  216. CBlasteroidsBulletSprite::CBlasteroidsBulletSprite(CSpriteWorld* world,CSpriteGame* game,short startTop,short startLeft,
  217.         short deltaV,short deltaH)
  218.     :    CGameSprite(world, game, 0, game->GetImage(5100), startTop, startLeft, 
  219.             game->GetImageMask(5100)),
  220.         fDeltaV(deltaV),
  221.         fDeltaH(deltaH)
  222. {
  223.     PlaySound(5001);
  224. }
  225.  
  226.  
  227. CBlasteroidsBulletSprite::~CBlasteroidsBulletSprite()//Override
  228.  
  229. {
  230.  
  231. }
  232.  
  233.  
  234. void    CBlasteroidsBulletSprite::UpdatePosition()//Override
  235. {
  236.     MoveBy(fDeltaH,fDeltaV);
  237.     if(OutOfBoundsQ()){
  238.         delete this;
  239.     }
  240. }
  241.